package com.qiji.video.web.controller;

import com.qiji.video.bean.Admins;
import com.qiji.video.exception.ServiceException;
import com.qiji.video.service.IAdminService;
import com.qiji.video.util.JwtUtil;
import com.qiji.video.util.Result;
import com.qiji.video.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 管理员个人中心模块
 */
@Api(
        tags = {"管理员个人中心模块"}
)
@RestController
@RequestMapping({"/auth/admin/info"})
public class AdminInfoController {
    @Autowired
    private IAdminService adminService;
    @ApiOperation("获取当前登录管理员信息")
    @GetMapping("/selectInfo")
    public Result findUserInfoByToken(HttpServletRequest request) throws ServiceException {

        String token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        Admins admins = adminService.currentUserInfo(Integer.valueOf(userId));


        return ResultUtil.success(admins);
    }
    @ApiOperation("修改当前登陆用户信息")
    @PostMapping("/updateinfo")
    public Result  updateAdminInfoByToken(HttpServletRequest request,Admins admins) throws ServiceException{

        String token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);

        adminService.updateAdminInfoByToken(Integer.valueOf(userId),admins);

        return ResultUtil.success();
    }
    @ApiOperation("修改当前登陆用户密码")
    @PostMapping("/updatePassword")
    public Result updateAdminPassword(HttpServletRequest request,String password) throws ServiceException{

        String token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        adminService.updateAdminPassword(Integer.valueOf(userId),password);

        return ResultUtil.success();
    }
}
